package com.landun.market.web.domain;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.landun.market.pojo.enums.StateCode;
import com.landun.market.pojo.model.Category;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.Optional;

/**
 * @author jiang.chuan
 * @date 2018-05-05
 */
@Data
@ToString
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@JsonPropertyOrder({"id", "name", "parentId", "type", "code", "rank", "level",
        "state", "createdTime", "updatedTime", "children"})
public class CategoryVO extends BaseVO<CategoryVO> {
    private Integer id;
    private String name;
    private Integer parentId;
    private Integer type;
    private Integer code;
    private Integer rank;
    private Integer level;
    private StateCode state;

    public CategoryVO(Category category) {
        Optional.ofNullable(category).ifPresent(cat -> {
            this.id = cat.getId();
            this.name = cat.getName();
            this.parentId = cat.getParentId();
            this.type = cat.getType();
            this.code = cat.getCode();
            this.rank = cat.getRank();
            this.level = cat.getLevel();
            this.state = cat.getState();
            this.setCreatedTime(cat.getCreatedTime());
            this.setUpdatedTime(cat.getUpdatedTime());
        });
    }

    public Category toEntity() {
        Category category = new Category();
        category.setId(this.id);
        category.setName(this.name);
        category.setParentId(this.parentId);
        category.setType(this.type);
        category.setCode(this.code);
        category.setRank(this.rank);
        category.setLevel(this.level);
        category.setState(this.state);
        return category;
    }
}
